#include <func.h>

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        puts("main filename");
        return -1;
    }

    DIR *pd = opendir(argv[1]);
    if (pd == NULL)
    {
        perror("opendir");
        return -1;
    }

    struct dirent* p;
    struct stat buf;
    int ret;
    while (p = readdir(pd))
    {
        char file_name[1024] = {0};
        sprintf(file_name, "%s%s%s", argv[1], "/", p->d_name);

        ret = stat(file_name, &buf);
        ERROR_CHECK(ret, -1, "stat");
        char mod_time[30] =  { 0 };

        strcpy(mod_time, ctime(&buf.st_mtime));
        char *pstr = mod_time, *ptr = mod_time;
        pstr += 10 * sizeof(char);
        ptr += 16 * sizeof(char);
        *ptr = '\0';

        printf("mode = %o %3ld %s %s %6ld %s %s\n", \
               buf.st_mode & 0xfff, buf.st_nlink, \
               getpwuid(buf.st_uid)->pw_name,\
               getgrgid(buf.st_gid)->gr_name, buf.st_size, \
              pstr, p->d_name);
    }
    
    closedir(pd);
    return 0;
}
